package com.kaili.controller.pda.v1;

import com.kaili.client.service.ILoginUserInfoService;
import com.kaili.common.exception.BaseExcept;
import com.kaili.common.util.CommonUtil;
import com.kaili.common.util.SnowflakeIdFactory;
import com.kaili.common.util.ValidList;
import com.kaili.db.service.IHsOptEwbAbnormalService;
import com.kaili.db.service.IHsOptEwbService;
import com.kaili.except.ResponseStatus;
import com.kaili.pojo.dto.abnormal.EwbAbnormalAddDTO;
import com.kaili.pojo.vo.ewbAbnormalVO;
import com.kaili.version.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @AUTHOR pengdongxu. @Date: 18-5-18 下午3:59. @COPYRIGHT 杭州凯立
 */
@ApiVersion(1)
@RequestMapping("/{api_version}")
@Api(tags = "[pda]到件异常", description = "[pda]到件异常服务API")
@RestController("ewbAbnormalControllerP")
public class EwbAbnormalController {

    @Autowired
    private MessageSource messageSource;
    @Autowired
    IHsOptEwbService ewbService;
    @Autowired
    private IHsOptEwbAbnormalService hsOptEwbAbnormalService;
    @Autowired
    ILoginUserInfoService loginUserInfoService;
    @Autowired
    IHsOptEwbService hsOptEwbService;


    @PostMapping("/p/ewbAbnormals/bat")
    @ApiOperation(value = "[pda]批量新增异常件端")
    List<Map<String, Object>> createdEwbAbnormalCS(@Validated @RequestBody ValidList<EwbAbnormalAddDTO> req) {
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < req.size(); i++) {
            EwbAbnormalAddDTO ewbAbnormalAddDTO = req.get(i);
            ResponseStatus responseStatus = createdEwbAbnormalBS(ewbAbnormalAddDTO);
            HashMap<String, Object> map = new HashMap<>();
            map.put("message", messageSource.getMessage(responseStatus.getMsg(), null, CommonUtil.getLocale()));
            map.put("status", responseStatus.getCode());
            map.put("ewbNo", ewbAbnormalAddDTO.getEwbNo());
            list.add(map);
        }
        return list;
    }


    public ResponseStatus createdEwbAbnormalBS(EwbAbnormalAddDTO req) {
        if (req.getEwbNo() != null) {
            Map<Object, Object> map = new HashMap<>();
            map.put("siteId", loginUserInfoService.getLoginSiteId());
            map.put("ewbNo", req.getEwbNo());
            Integer total = ewbService.getInfoByEwbNo(map);
            //校验操作网点是否为目的网点
            if (total == null) {
                return ResponseStatus.EXCEPTION_EWB_NONE;
            } else {
                if (req.getDamagePiece() == null) {
                    req.setDamagePiece(0);
                }
                if (req.getDiffPiece() == null) {
                    req.setDiffPiece(0);
                }
                if (req.getDamagePiece() + req.getDiffPiece() > total) {
                    return ResponseStatus.TOTAL_AMOUNT_IS_GREATER;
                }
            }
        } else {
            return ResponseStatus.EWB_ISEMPTY;
        }

        // 重复申报校验
        ewbAbnormalVO abnormal = hsOptEwbAbnormalService.queryAbnormalByEwbNo(req);
        if (abnormal != null) {
            return ResponseStatus.RESPONSE_CHECK_ERROR;
        }
        try {
            String operationId = SnowflakeIdFactory.getNextId();
            hsOptEwbAbnormalService.createdEwbAbnormalBS(req);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseExcept(ResponseStatus.EXCEPTION_ADD_ERR);
        }
        return ResponseStatus.RESPONSE_OK;
    }


}
